class wm_SpeechRecognition:
    # 传入uart、AW9523的一个示例
    def __init__(self, uart=None, aw9523=None):
        # 先设置uart
        if uart is None:
            try:
                import __main__
                if hasattr(__main__, 'uart'):
                    self.uart = __main__.uart
                else:
                    raise ValueError("未找到UART对象，请先初始化UART")
            except Exception as e:
                raise ValueError(f"未找到UART对象，请先初始化UART: {e}")
        else:
            self.uart = uart

        # 再设置aw9523
        if aw9523 is None:
            try:
                import __main__
                if hasattr(__main__, 'aw'):
                    self.aw9523 = __main__.aw
                else:
                    raise ValueError("未找到AW9523对象，请先初始化AW9523")
            except Exception as e:
                raise ValueError(f"未找到AW9523对象，请先初始化AW9523: {e}")
        else:
            self.aw9523 = aw9523

        # 默认使能
        try:
            self.aw9523.set_pin(1, 0, True)
            print("语音识别模块初始化成功")
        except Exception as e:
            print(f"语音识别模块初始化警告: {e}")

    def enable(self):
        """使能语音识别模块"""
        try:
            self.aw9523.set_pin(1, 0, True)
            print("语音识别模块已使能")
        except Exception as e:
            print(f"使能失败: {e}")

    def disable(self):
        """禁用语音识别模块"""
        try:
            self.aw9523.set_pin(1, 0, False)
            print("语音识别模块已禁用")
        except Exception as e:
            print(f"禁用失败: {e}")
        
    def read(self):
        """读取语音识别数据"""
        try:
            if self.uart.any():
                v = self.uart.read()
                if v:
                    return v[0]
            return 0
        except Exception as e:
            print(f"读取失败: {e}")
            return 0
